<?php
/**
 * oscshop2 B2C电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015-2017 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 * @author   test
 *
 */
namespace osc\admin\controller;
use osc\common\controller\AdminBase;
use think\Db;
use think\Request;
class Fin extends AdminBase{
	
	protected function _initialize(){
		parent::_initialize();
		$this->assign('breadcrumb1','财务管理');
		$this->assign('breadcrumb2','提现');		
	}

	function index()
	{


		$param=input('param.');
		
		$query=[];
		
		if(isset($param['user_name'])){		
			$map['m.username|m.nickname']=['like',"%".$param['user_name']."%"];
			$query['m.username']=urlencode($param['user_name']);
		}
		/*else{
			$map['m.uid']=['gt',0];
		}
		*/
		
		$list=[];
		
		$list=Db::name('goldstonewd')->alias('m')->field('m.*')->where('pay_status','0')
		->order('m.creat_time desc')->paginate(config('page_num'),false,$query);		
		
		$this->assign('list',$list);
				
		$this->assign('empty','<tr><td colspan="20">没有数据~</td></tr>');
		
    	return $this->fetch();



				//获取未提现的记录
		//$wd=Db::name('auth_group_access')->where('pay_status','1')->order('creat_time desc')->select();


		//return $this->fetch();
	}

	/**
	 * 积分元宝数据统计
	 * @return mixed
	 */
	function integral(){

		$data = $this->request->param();

		$map = [];
		if(isset($data['begin_time'])){
			$map['dt'] = ['>=',$data['begin_time']];
		}

		if(isset($data['end_time'])){
			$map['dt'] = ['<=',$data['end_time']];
		}


		$list = Db::name('integral')->where($map)->paginate(10,false, [
				'query' => Request::instance()->param(),//不丢失已存在的url参数
		]);

		$this->assign('list',$list);
		$this->assign('breadcrumb1','数据统计');
		$this->assign('breadcrumb2','积分元宝数据统计');

		return $this->fetch();
	}

	/**
	 * 即时查询
	 * @return array
	 */
	function immediate(){
		if($this->request->isPost()){

			$param = $this->request->param();

			if($param['token'] != '4564112sda'){
				return ['error'=> '查询错误'];
			}

			$data = [
				'bao'			=> Db::name('member')->sum('bao'),
				'jilt'			=> Db::name('member')->sum('jilt'),
				'jilin_bean'	=> Db::name('member')->sum('jilin_bean'),
				'ticket'	=> Db::name('member')->sum('ticket'),
				'predeposit'	=> Db::name('member')->sum('predeposit'),
				'b2_predeposit'	=> Db::name('member')->sum('b2_predeposit'),
			];

			return ['success'=>'OK','data'=>$data];
		}else{
			return ['error'=> '查询错误'];
		}
	}

	/**
	 * 会员奖励
	 * @return mixed
	 */
	function account()
	{


		$param=input('param.');
		$query=[];
		$map=null;
		
		if(isset($param['user_name'])){		
			$map['m.username']=['like',"%".$param['user_name']."%"];
			$query['m.username']=urlencode($param['user_name']);
		}
		if(isset($param['begin_time'])){
			//$param['begin_time'] = strtotime($param['begin_time']);		
			$map['create_time']=['>',strtotime($param['begin_time'])];
			$query['m.begin_time']=urlencode($param['begin_time']);
		}
		//var_dump($map);
		if(isset($param['end_time'])){
			//$param['begin_time'] = strtotime($param['begin_time']);		
			$map['m.create_time']=['<',strtotime($param['end_time'])];
			//var_dump(strtotime($param['end_time']));
			
			$query['m.end_time']=urlencode($param['end_time']);
		}

		$list=[];
		$sum = 0;
		if($map)
		{
			$list=Db::name('bonus')->alias('m')->field('m.*')->where($map)->order('m.create_time desc')->paginate(50,false, [
                'query' => Request::instance()->param(),//不丢失已存在的url参数
            ]);
			$sum=Db::name('bonus')->alias('m')->field('m.*')->where($map)->sum('num');
		}else
		{
			$list=Db::name('bonus')->alias('m')->field('m.*')->order('m.create_time desc')->paginate(50,false, [
                'query' => Request::instance()->param(),//不丢失已存在的url参数
            ]);
			$sum=Db::name('bonus')->alias('m')->field('m.*')->sum('num');
		}
				
		
		$this->assign('list',$list);
		$this->assign('sum',$sum);
				
		$this->assign('empty','<tr><td colspan="20">没有数据~</td></tr>');
		$this->assign('breadcrumb1','数据统计');
		$this->assign('breadcrumb2','会员奖励');
		
    	return $this->fetch();
	}

	function agent()
	{
		$param=input('param.');
		$query=[];
		$map=null;
		
		if(isset($param['user_name'])){		
			$map['m.username']=['like',"%".$param['user_name']."%"];
		}

		if(isset($param['begin_time'])){
			//$param['begin_time'] = strtotime($param['begin_time']);		
			$map['creat_time']=['>',strtotime($param['begin_time'])];
		}
		//var_dump($map);
		if(isset($param['end_time'])){
			//$param['begin_time'] = strtotime($param['begin_time']);		
			$map['m.creat_time']=['<',strtotime($param['end_time'])];
			//var_dump(strtotime($param['end_time']));
		}

		//var_dump($map);
		//die;
		$list=[];
		$sum = 0;
		if($map)
		{
			$list=Db::name('agent_account')->alias('m')->field('m.*')->where($map)->order('m.creat_time desc')->paginate(20,false, [
							'query' => Request::instance()->param(),//不丢失已存在的url参数
					]);
			$sum = Db::name('agent_account')->alias('m')->field('m.*')->where($map)->sum('num');
		}else
		{
			$list=Db::name('agent_account')->alias('m')->field('m.*')->order('m.creat_time desc')->paginate(20,false, [
					'query' => Request::instance()->param(),//不丢失已存在的url参数
			]);
			$sum = Db::name('agent_account')->alias('m')->field('m.*')->sum('num');
		}
				
		
		$this->assign('list',$list);
		$this->assign('sum',$sum);
				
		$this->assign('empty','<tr><td colspan="20">没有数据~</td></tr>');
		$this->assign('breadcrumb1','数据统计');
		$this->assign('breadcrumb2','代理奖励');
		
    	return $this->fetch();
	}

	function account_num()
	{
		$param=input('param.');
				
		if(isset($param['dt'])){		
			$sql = "select sum(num) co, b.reg from (select FROM_UNIXTIME(create_time,'%Y-%m-%d') reg, a.*  from osc_bonus  a) b  where b.reg = '".$param['dt']."' group by b.reg order by b.reg desc";
			/*var_dump($sql);
			die;*/

		}else
		{
			$sql = "select sum(num) co, b.reg from (select FROM_UNIXTIME(create_time,'%Y-%m-%d') reg, a.*  from osc_bonus a) b group by b.reg order by b.reg desc";


		}
		
		$list = DB::query($sql);
		$this->assign('list',$list);

				
		$this->assign('empty','<tr><td colspan="20">没有数据~</td></tr>');
		$this->assign('breadcrumb1','数据统计');
		$this->assign('breadcrumb2','会员奖励数据统计');
		
    	return $this->fetch();
	}

	function member()
	{
		$param=input('param.');
				
		$num0 = DB::name('member')->where('user_state',0)->count();
		$num1 = DB::name('member')->where('user_state > 0')->count();
		$num2 = DB::name('member')->count();

		if(isset($param['dt'])){		
			$sql = "select count(*) co, b.reg from (select FROM_UNIXTIME(regdate,'%Y-%m-%d') reg, a.*  from osc_member  a) b  where b.reg = '".$param['dt']."' group by b.reg order by b.reg desc";
			//var_dump($sql);
			//die;

		}else
		{
			$sql = "select count(*) co, b.reg from (select FROM_UNIXTIME(regdate,'%Y-%m-%d') reg, a.*  from osc_member  a) b group by b.reg order by b.reg desc";
		}
		$co = DB::name('member')->sum('predeposit');
		
		$list = DB::query($sql);
		$this->assign('num0',$num0);
		$this->assign('num1',$num1);
		$this->assign('num2',$num2);
		$this->assign('list',$list);
		$this->assign('co',$co);
				
		$this->assign('empty','<tr><td colspan="20">没有数据~</td></tr>');
		$this->assign('breadcrumb1','数据统计');
		$this->assign('breadcrumb2','会员数据统计');
		
    	return $this->fetch();
	}

	function order()
	{
		$param=input('param.');
				
		if(isset($param['dt'])){		
			$sql = "select count(*) co, b.reg from (select FROM_UNIXTIME(pay_time,'%Y-%m-%d') reg, a.*  from osc_order  a where a.order_status_id<>3) b  where b.reg = '".$param['dt']."' group by b.reg order by b.reg desc";
			//var_dump($sql);
			//die;

		}else
		{
			$sql = "select count(*) co, b.reg from (select FROM_UNIXTIME(pay_time,'%Y-%m-%d') reg, a.*  from osc_order  a  where a.order_status_id<>3) b group by b.reg order by b.reg desc";
		}
		
		$list = DB::query($sql);
		$this->assign('list',$list);

				
		$this->assign('empty','<tr><td colspan="20">没有数据~</td></tr>');
		$this->assign('breadcrumb1','数据统计');
		$this->assign('breadcrumb2','订单数据统计');
		
    	return $this->fetch();
	}

	/**
	 * 财务充值
	 * @return mixed
	 */
	function recharge()
	{
		$param=input('param.');
		if(!isset($param['username']))
		{
			$param['type']='0';
		}

		$paramToken = $this->request->param('__token__');
		$token = Db::name('token')->where('token',$paramToken)->find();
		if($token){
			$this->error('不可重复提交');
		}


//		print_r($token);exit;
		$error=null;
		if($param['type']=='1')
		{
				if(isset($param['username'])){		
					//$map['m.username|m.nickname']=['like',"%".$param['user_name']."%"];
					//$query['m.username']=urlencode($param['username']);

					$user = DB::name('member')->where('username',$param['username'])->find(); 



					$error=null;
					
					if(!$user)
					{
						$error="用户不存在";
					}else
					{
						 if(is_numeric($param['num']))
						 {
						 	if(($param['num'] + $user['predeposit']) < 0){
						 		$error = '可用金豆不足，当前用户金豆余额为'.$user['predeposit'];
							}else{
								$user = DB::name('member')->where('username',$param['username'])->setInc('predeposit',$param['num']);
								$data['username']= $param['username'];
								$data['num']= $param['num'];
								$data['dt']= time();
								$data['admin']= session('adminuser');

								DB::name('user_recharge')->insert($data);
								Db::name('token')->insert(['token'=>$paramToken]);
							}
								
						}

					}
				}
		}
		$list=null;
		if($param['type']=='2')
		{
			$list = DB::name('user_recharge')->where('username',$param['user'])->order('id desc')->select();
		}else
		{
			$list = DB::name('user_recharge')->order('id desc')->select();
		}
		

		
		$this->assign('list',$list);
		$this->assign('error',$error);
		$this->assign('empty','<tr><td colspan="20">没有数据~</td></tr>');
		$this->assign('breadcrumb1','财务管理');
		$this->assign('breadcrumb2','充值');
		return $this->fetch();
	}


	

	

	function isendwd()
	{
		$param=input('param.');
		
		$query=[];
		
		if(isset($param['user_name'])){		
			$map['m.username|m.nickname']=['like',"%".$param['user_name']."%"];
			$query['m.username']=urlencode($param['user_name']);
		}
		/*else{
			$map['m.uid']=['gt',0];
		}
		*/
		
		$list=[];
		
		$list=Db::name('goldstonewd')->alias('m')->field('m.*')->where('pay_status','1')
		->order('m.creat_time desc')->paginate(config('page_num'),false,$query);


		$this->assign('breadcrumb1','财务管理');
		$this->assign('breadcrumb2','已处理');			
		
		$this->assign('list',$list);
				
		$this->assign('empty','<tr><td colspan="20">没有数据~</td></tr>');
		
    	return $this->fetch();

	}

	function isprocing()
	{
		$param=input('param.');
		$this->assign('breadcrumb1','财务管理');
		$this->assign('breadcrumb2','正在处理');	
		
		$query=[];
		
		if(isset($param['user_name'])){		
			$map['m.username|m.nickname']=['like',"%".$param['user_name']."%"];
			$query['m.username']=urlencode($param['user_name']);
		}
		/*else{
			$map['m.uid']=['gt',0];
		}
		*/
		
		$list=[];
		
		$list=Db::name('goldstonewd')->alias('m')->field('m.*')->where('pay_status','3')
		->order('m.creat_time desc')->paginate(config('page_num'),false,$query);		
		
		$this->assign('list',$list);
				
		$this->assign('empty','<tr><td colspan="20">没有数据~</td></tr>');
		
    	return $this->fetch();

	}

	function iscancel()
	{
		$param=input('param.');
		$this->assign('breadcrumb1','财务管理');
		$this->assign('breadcrumb2','已取消');	
		
		$query=[];
		
		if(isset($param['user_name'])){		
			$map['m.username|m.nickname']=['like',"%".$param['user_name']."%"];
			$query['m.username']=urlencode($param['user_name']);
		}
		/*else{
			$map['m.uid']=['gt',0];
		}
		*/
		
		$list=[];
		
		$list=Db::name('goldstonewd')->alias('m')->field('m.*')->where('pay_status','2')
		->order('m.creat_time desc')->paginate(config('page_num'),false,$query);		
		
		$this->assign('list',$list);
				
		$this->assign('empty','<tr><td colspan="20">没有数据~</td></tr>');
		
    	return $this->fetch();
	}

	function pay()
	{
		$data=input('get.');
		if($data['token'])
		{
			Db::name('goldstonewd')->where('token',$data['token'])->update(['pay_status'=>'1']);
		}
		$this->redirect('fin/index');
	}

	function getuser()
	{
		$data=input('get.');
		//var_dump(expression)
		if($data['username'])
		{
			$ret = Db::name('member')->where('username',$data['username'])->find();
			return "确认充值会员：".$ret['idcardname'];
		}
		//$this->redirect('fin/index');
	}

	function cancel()
	{
		$data=input('get.');
		$ret = Db::name('goldstonewd')->where('token',$data['token'])->where(['pay_status'=>'0'])->find();
		if($ret)
		{
			Db::name('goldstonewd')->where('token',$data['token'])->update(['pay_status'=>'2']);
			Db::name('member')->where('username',$ret['username'])->setInc('predeposit',$ret['num']);
		}
		$this->redirect('fin/index');
		
		
	}


	


}
?>